﻿using System.ComponentModel;
using System.Windows.Forms;
using WindowsAPI.Hooks;
using WindowsAPI.Windows;

namespace Test_Form
{
    public partial class MainForm : Form
    {
        private Window f_Window;
        private Hook f_Hook;

        private readonly BindingList<WM> f_DataList =
            new BindingList<WM>();

        public MainForm()
        {
            InitializeComponent();

            Data_ListBox.DataSource = f_DataList;
        }

        private void OnWindowsListDropDown(object sender, System.EventArgs e)
        {
            WindowsList_ComboBox.DataSource =
                Window.FindWindow(w => !string.IsNullOrEmpty(w.Text));
        }

        private void FindWindow_Button_Click(object sender, System.EventArgs e)
        {
            var ww = Window.FindWindow(w => w.Text.Contains(WindowsCaption_TextBox.Text));
            if(ww == null || ww.Length == 0) return;
            f_Window = ww[0];

            if(f_Hook != null && f_Hook.Enable)
            {
                f_Hook.Dispose();
                f_Hook = null;
                Start_CheckBox.Checked = false;
            }

            f_DataList.Clear();
            f_Hook = f_Window.SetHook();
            f_Hook.Event += Hook_Event;
            Start_CheckBox.Checked = true;
        }

        private void Start_CheckBox_CheckedChanged(object sender, System.EventArgs e)
        {
            if(f_Hook == null) return;
            f_Hook.Enable = Start_CheckBox.Checked;

        }

        private void Hook_Event(object sender, HookEventArgs e)
        {
            f_DataList.Insert(0, (WM)e.wParams);
        }

        private void WindowsList_ComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            WindowsCaption_TextBox.Text =
                        ((Window)WindowsList_ComboBox.SelectedItem).Text;
        }

        private void button1_Click(object sender, System.EventArgs e) { }
    }
}
